/**
* Alert management class/functions
*/

//some states---entire state model is not represented
//states condensed

const SMACK_STATE_INIT = "state_init";
const SMACK_STATE_CONTEST_END = "state_contest_end";
const SMACK_STATE_OW_OPTIN = "state_ow_optin";
const SMACK_STATE_OW_VOTE = "state_ow_vote";
const SMACK_STATE_GG = "state_gg";
const SMACK_STATE_DEAL = "state_deal";
const SMACK_STATE_GG_DEAL = "state_gg_deal";

//the various deal div className values
const SMACK_DEAL = "";
const SMACK_DEAL_PREVIEW = "preview";
const SMACK_DEAL_OUTWIT = "outwit";
const SMACK_DEAL_WHEEL = "wheel";

//'chrome://swaksmack/content/ggcalc.html','SwakSmack Guru Calculator','height=400,width=500');
var SS_disposeId = null;
function SSAlertManager() {
	this.outwitAlertHeight = 200;
	this.outwitAlertWidth = 400;
	this.alertWindow = null;
	this.quickClose = true;
	this.state = SMACK_STATE_INIT;
	//var jellyfishstrings = document.getElementById("swaksmack-strings");
	var alert1 = SS_getStringTableText("outwitWarn1");
	this.outwitOptInText = alert1

	alert1 = SS_getStringTableText("outwitVoteWarn1");
	this.outwitVoteText = alert1;

	alert1 = SS_getStringTableText("guruguessWarn1");
	this.guruGuessAlert = alert1;
	
	alert1 = SS_getStringTableText("smackPotWarn1");
	this.smackPotAlert = alert1;
	
	
	alert1 = SS_getStringTableText("buyAlert");
	this.buyAlert = alert1;
	
	alert1 = SS_getStringTableText("wheelSpinAlert");
	this.wheelSpinAlert = alert1;

	this.disposeId = null;
	//alert types
	//1: outwit optin
	//2: outwit vote
	//10: guru guess reminder
	this.alertType = null;

	this.updateAlertState = function(doc) {
		//alert('updateAlertState entry');
		//not a true state machine by any means, just a state tracker if you will
		if (doc == null) return;
		//alert('update alert state');
		var timerdiv = doc.getElementById('liveObjectTimer');
		var ggdiv = doc.getElementById('guruContainer');
		var dealdiv = doc.getElementById('deal');

		if (timerdiv != null || dealdiv.className == SMACK_DEAL_OUTWIT) {
			//optin or ow vote state
			var capdiv = doc.getElementById(SMACK_OUTWITCAPDIV);
			if (capdiv != null) {
				if (capdiv.style.display == "none") {
					this.state = SMACK_STATE_CONTEST_END;
				}
				else {
					this.state = SMACK_STATE_OW_OPTIN;
				}
			}
			else if (doc.getElementById(SMACK_OUTWITANSWERDIV) != null) {
				//alert('ow alert state');
				this.state = SMACK_STATE_OW_VOTE;
			}
			else {
				this.state = SMACK_STATE_CONTEST_END;
			}
		}
		else if(ggdiv != null) {
			if (ggdiv.style.display == "none") {
				//no guru info displayed
				this.state = SMACK_STATE_DEAL;
			}
			else if (ggdiv.style.display == "block") {
				//check for submitted guess or missed entry
				if (doc.getElementById('guruGuessingClosed') != null ||
					  			doc.getElementById('guruYourGuess') != null) {
					//either you entered a guess or you missed it
					this.state = SMACK_STATE_GG_DEAL;
				}
				else {
					//guru guess entry valid 
					this.state = SMACK_STATE_GG;
				}
			}
		} //end ggdiv !=null
		else {
			this.state = SMACK_STATE_DEAL;
		}

	} //end updateAlertState

		this.swakAlert = function(alertType, autoDispose, disposeTime, quickClose) {
			if (this.isAlertWindowClosed() == false) {
				//close the current window

				this.alertWindow.close();
			}
			if (autoDispose == null) autoDispose = true;
			if (disposeTime == null) disposeTime = 5000;
			if (quickClose == null) quickClose = true;
			this.quickClose = quickClose;
			this.alertType = alertType;

			/*
			var w = 640, h = 480;

			w = window.outerWidth;
			h = window.outerHeight;

			var popW = 400, popH = 300;

			var leftPos = (w-popW)/2, topPos = (h-popH)/2;
			*/
			
			var title1 = 'SwackSmack Alert';

			this.alertWindow = window.open("chrome://swaksmack/content/alert.xul", title1, "chrome,centerscreen,width=400,height=200");
			//var size = 'width=' + popW + ',height=' + popH + ',top='+topPos + ',left=' + leftPos;
			
			//this.alertWindow = SS_window.open("chrome://swaksmack/content/alert.xul",title1,'chrome, width=' + popW + ',height='+popH+',top='+topPos+',left='+leftPos);
			if (autoDispose) {
				SS_disposeId = setTimeout('swaksmack.alertManager.monitor()',disposeTime);
				this.disposeId = SS_disposeId;
			}

			this.alertWindow.addEventListener('load', function(event) {
				//var doc = event.currentTarget.document;
				//var a1 = doc.getElementById('SS-alert-text1');

				swaksmack.alertManager.finishAlertSetup();
				swaksmack.alertManager.alertWindow.document.getElementById('SS-alert-ok-button').focus();
				//SS_AM.alertWindow.focus();

			}, true);

			/*
			if (this.quickClose == true) {
				///this.alertWindow.addEventListener('blur', function(event) {
				window.addEventListener('focus', function(event) {	

					try{
						if ( gBrowser.contentWindow.document.location == 'http://www.jellyfish.com/smacktv/channel/1') {
							//swaksmack.alertManager.alertWindow.close();
						}
					//alert('window ' + gBrowser.contentWindow.document.location);
				}catch(Exception){}
					//SS_AM.alertWindow.close();

				}, true);
			}
			*/

			this.alertWindow.addEventListener('unload', function(event) {
				//alert(event.currentTarget);
				if (event.currentTarget.closed ==  true) {
					swaksmack.alertManager.dispose();
				}
			}, true);



		}

		this.dispose = function(){
			if (this.disposeId == null) return;
			clearTimeout(this.disposeId);
			this.disposeId = null;
		}

		this.monitor = function() {
			if (this.alertWindow == null || this.alertWindow.closed == true) {
				return;
			}
			else {
				this.alertWindow.close();
			}
		}

		this.alertPresent = function(alertType) {
			if (this.isAlertWindowClosed()) return false;
			if (alertType == null) return false;
			if (this.alertType == null) return false;
			if (this.alertType == alertType ) return true;

		}

		this.isAlertWindowClosed = function() {
			if (this.alertWindow == null || this.alertWindow.closed == true) return true;
			return false;
		}

		this.finishAlertSetup = function() {
			//SS_AM.alertWindow.document.getElementById('SS-alert-ok-button').focus();
			var a1 = swaksmack.alertManager.alertWindow.document.getElementById('SS-alert-text1');
			//double check
			if (a1 == null) return;
			
						
			switch (this.alertType) {
				case SMACK_ALERT_OUTWITOPTIN:
					a1.textContent = this.outwitOptInText;
					break;
				case SMACK_ALERT_OUTWITVOTE:
					var alert2 = this.outwitVoteText;
					var randomGuess = Math.ceil(Math.random()*6);
					var outwitRandomPref = true;
					try {
						outwitRandomPref = SS_getPreference(SMACK_OUTWITRANDOM);
					} catch(Exception){}
					if (outwitRandomPref){
						alert2 += "\n" + SS_getStringTableText("outwitRandom1") + randomGuess;
					}
					a1.textContent = alert2;
					break;
				case SMACK_ALERT_GURUGUESS:
					a1.textContent = this.guruGuessAlert;
					break;
				case SMACK_ALERT_SMACKPOTGUESS:
					a1.textContent = this.smackPotAlert;
					break;
				case SMACK_ALERT_BUYALERT:
          var buyAlertTxt = this.buyAlert;
				  buyAlertTxt += "!!!\n" + getProduct() +"!";
					a1.textContent = buyAlertTxt;
				  break;
				case SMACK_ALERT_GURUWHEELSPIN:
					a1.textContent = this.wheelSpinAlert;
					break;    
				case SMACK_ALERT_CUSTOM:
					a1.textContent = "DEBUG: Bugs make Hulk angry...Hulk smash bugs good.";
					break;						
				default : a1.textContent = "Unknown SwakSmack Alert";
				}

			}





		}

/*
		function closeAlert(aWindow) {
			var gen = aWindow.opener;
			alert('gen close alert gen.SS_doc ss_am ' + gen.SS_doc.SS_AM);
		}
		*/
